﻿using System;
using System.Net;

namespace MyMailWp8.Protocol
{
    internal class CMPopLogin : CMPopBase
    {
        private int loginState = 0;//

        public CMPopLogin(AsyncSocketClient sockClient)
            : base(sockClient)
        { }

        override public int ParseData(String inString)
        {
            if (inString == null)
                return 0;

            switch (loginState)
            {
                //+OK China Mobile push proxy v2.0.0 ready
                case 0:
                    //waiting for OK message
                    if (inString.ToUpper().StartsWith("+OK"))
                    {
                        loginState = 1;
                        // Account account = GetAccountInfo();
                        if (account != null)
                        {
                            SendData("USER " + account.emailAddress + " 2.1.0 GC001 5.5B20111121\r\n");
                            //    SendData("USER 13910295587@139.com 2.0.0 GC001 5.5B20111121\r\n");
                        }
                        else
                        {
                            //DefaultLogger.Log.LogDebug("can't get account info");
                            ////
                        }
                    }
                    else
                    {
                        //DefaultLogger.Log.LogDebug(inString);
                        //DefaultLogger.Log.LogDebug("not receive welcome message");
                        PostServerRetError((int)SockMessageType.ConnectFail);
                    }
                    break;

                //+OK LT-V2.0
                case 1:
                    if (inString.ToUpper().StartsWith("+OK"))
                    {
                        loginState = 2;

                        Account account = GetAccountInfo();
                        if (account != null)
                        {
                            SendData("PASS " + account.password + "\r\n");
                            //SendData("PASS ****\r\n");
                        }
                        else
                        {
                            //DefaultLogger.Log.LogDebug("can't get account info");
                        }
                    }
                    else
                    {
                        PostServerRetError((int)SockMessageType.LoginFail);
                    }
                    break;

                //+OK maildrop locked and ready
                case 2:
                    if (inString.ToUpper().StartsWith("+OK") == true)
                    {
                        loginState = 10;
                        int nState = (int)(CMPopState.CMPOP_List);
                        ChangeCMPopSmtpState(nState);
                    }
                    else
                    {
                        //DefaultLogger.Log.LogDebug("\r\nnot receive +OK maildrop locked and ready\r\n");
                        PostServerRetError((int)SockMessageType.LoginFail);
                    }
                    break;
                default:
                    break;
            }

            return 0;
        }
    }//cls
}
